package com.txby.common.utils.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.junit.Test;

/**
 * 日期的加减等运算
* @ClassName: DateAirth 
* @Description: TODO （描述）
* @author 作者 jacking
* @date 2016-7-5 上午9:30:17
 */
public class DateAirth {
    /**
     * yyyyMMdd
     */
    public static final String YYYYMMDD_8 = "yyyyMMdd";
    /**
     * yyyy-MM-dd
     */
    public static final String YYYYMMDD_10 = "yyyy-MM-dd";
    /**
     * yyyy-MM-dd HH:mm
     */
    public static final String YYYYMMDDHHMM_16 = "yyyy-MM-dd HH:mm";
    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public static final String YYYYMMDDHHMMSS_19 = "yyyy-MM-dd HH:mm:ss";
    /**
     * yyyy-MM-dd
     */
    private static final String DEFAULT_PATTERN = YYYYMMDD_10;
    
    
    /**
     * 计算当前年加减
     * @param years + or -1数字
     * @return
     */
	public static String getYears(int years) {
	    Date d = new Date();
		GregorianCalendar gc = new GregorianCalendar();
		SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDDHHMMSS_19);
		gc.setTime(d);
		gc.add(1, years);
		gc.set(gc.get(Calendar.YEAR), gc.get(Calendar.MONTH),
				gc.get(Calendar.DATE));
		return sdf.format(gc.getTime());
	}
	/**
	 * 获取指定日期(年月日 时分秒)的加减日期
	 * @param date 字符串日期 yyyy-MM-dd HH:mm:ss
	 * @param years + or - 所有参数如此
	 * @param months 
	 * @param days 
	 * @param hours
	 * @param minutes
	 * @param seconds
	 * @return  日期
	 * @throws ParseException
	 */
	public static String getAirthStringDate(String date,int years,int months,int days,int hours,int minutes,int seconds) throws ParseException {
		SimpleDateFormat sdf=new SimpleDateFormat(YYYYMMDDHHMMSS_19);
		Date willDate=new Date();
		if(null!=date&&!"".equals(date)){
			try {
				
				willDate=sdf.parse(date);
			} catch (Exception e) {
				e.printStackTrace();
				return "日期应当为：yyyy-MM-dd HH:mm:ss";
			}
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(willDate);
		cal.add(cal.YEAR,years);
		 cal.add(cal.MONTH,months);
		 cal.add(cal.DAY_OF_MONTH,days);
		 cal.add(cal.HOUR_OF_DAY,hours);
		 cal.add(cal.MINUTE,minutes);
		 cal.add(cal.SECOND,seconds);
		String d=sdf.format(cal.getTime());
		return d;
	}
	/**
	 * 获取指定日期(时分秒)的加减日期
	 * @param date 字符串日期 yyyy-MM-dd HH:mm:ss
	 * @param years + or - 所有参数如此
	 * @param months 
	 * @param days 
	 * @param hours
	 * @param minutes
	 * @param seconds
	 * @return  日期
	 * @throws ParseException
	 */
	public static String getAirthStringDateHMS(Date date,int hours,int minutes,int seconds) throws ParseException {
		SimpleDateFormat sdf=new SimpleDateFormat(YYYYMMDDHHMMSS_19);
		Date willDate=new Date();
		if(null!=date&&!"".equals(date)){
			try {
				
				willDate=date;
			} catch (Exception e) {
				e.printStackTrace();
				return "日期应当为：yyyy-MM-dd HH:mm:ss";
			}
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(willDate);
		cal.add(cal.HOUR_OF_DAY,hours);
		cal.add(cal.MINUTE,minutes);
		cal.add(cal.SECOND,seconds);
		String d=sdf.format(cal.getTime());
		return d;
	}
	/**
	 * 获取指定日期(时分)的加减日期
	 * @param date 字符串日期 yyyy-MM-dd HH:mm:ss
	 * @param years + or - 所有参数如此
	 * @param months 
	 * @param days 
	 * @param hours
	 * @param minutes
	 * @param seconds
	 * @return  yyyy-MM-dd日期
	 * @throws ParseException
	 */
	public static String getAirthStringDateHM(Date date,int hours,int minutes) throws ParseException {
		SimpleDateFormat sdf=new SimpleDateFormat(YYYYMMDD_10);
		Date willDate=new Date();
		if(null!=date&&!"".equals(date)){
			try {
				willDate=date;
			} catch (Exception e) {
				e.printStackTrace();
				return "日期应当为：yyyy-MM-dd";
			}
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(willDate);
		cal.add(cal.HOUR_OF_DAY,hours);
		cal.add(cal.MINUTE,minutes);
		String d=sdf.format(cal.getTime());
		return d;
	}
	/**
	 * 获取指定日期(年月日 时分秒)的加减日期
	 * @param date 
	 * @param years + or - 所有参数如此
	 * @param months 
	 * @param days 
	 * @param hours
	 * @param minutes
	 * @param seconds
	 * @return  日期
	 * @throws ParseException
	 */
	public static Date getAirthMyDate(Date date,int years,int months,int days,int hours,int minutes,int seconds) throws ParseException {
		Date willDate=new Date();
		if(null!=date&&!"".equals(date)){
			try {
				willDate=date;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(willDate);
		cal.add(cal.YEAR,years);
		cal.add(cal.MONTH,months);
		cal.add(cal.DAY_OF_MONTH,days);
		cal.add(cal.HOUR_OF_DAY,hours);
		cal.add(cal.MINUTE,minutes);
		cal.add(cal.SECOND,seconds);
		return cal.getTime();
	}
	/**
	 * 获取指定日期(年月日 )的加减日期
	 * @param date 
	 * @param years + or - 所有参数如此
	 * @param months 
	 * @param days 
	 * @param hours
	 * @param minutes
	 * @param seconds
	 * @return  日期
	 * @throws ParseException
	 */
	public static Date getAirthMyDate(Date date,int years,int months,int days) throws ParseException {
		Date willDate=new Date();
		if(null!=date&&!"".equals(date)){
			try {
				willDate=date;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(willDate);
		cal.add(cal.YEAR,years);
		cal.add(cal.MONTH,months);
		cal.add(cal.DAY_OF_MONTH,days);
		return cal.getTime();
	}
	/**
	 * 获取指定日期(年月日 )的加减日期
	 * @param date 字符串日期 yyyy-MM-dd HH:mm:ss
	 * @param years + or - 所有参数如此
	 * @param months 
	 * @param days 
	 * @param hours
	 * @param minutes
	 * @param seconds
	 * @return  日期
	 * @throws ParseException
	 */
	public static String getAirthStringDate(String date,int years,int months,int days) throws ParseException {
		SimpleDateFormat sdf=new SimpleDateFormat(YYYYMMDDHHMMSS_19);
		Date willDate=new Date();
		if(null!=date&&!"".equals(date)){
			try {
				
				willDate=sdf.parse(date);
			} catch (Exception e) {
				e.printStackTrace();
				return "日期应当为：yyyy-MM-dd HH:mm:ss";
			}
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(willDate);
		cal.add(cal.YEAR,years);
		cal.add(cal.MONTH,months);
		cal.add(cal.DAY_OF_MONTH,days);
		String d=sdf.format(cal.getTime());
		return d;
	}
	/**
	 * 获取指定日期(年月日 )的加减日期
	 * @param date 字符串日期 yyyy-MM-dd HH:mm:ss
	 * @param years + or - 所有参数如此
	 * @param months 
	 * @param days 
	 * @param hours
	 * @param minutes
	 * @param seconds
	 * @return  日期
	 * @throws ParseException
	 */
	public static String getAirthStringDate(Date date,int years,int months,int days) throws ParseException {
		SimpleDateFormat sdf=new SimpleDateFormat(YYYYMMDDHHMMSS_19);
		Date willDate=new Date();
		if(null!=date&&!"".equals(date)){
			try {
				
				willDate=date;
			} catch (Exception e) {
				e.printStackTrace();
				return "日期应当为：yyyy-MM-dd HH:mm:ss";
			}
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(willDate);
		cal.add(cal.YEAR,years);
		cal.add(cal.MONTH,months);
		cal.add(cal.DAY_OF_MONTH,days);
		String d=sdf.format(cal.getTime());
		return d;
	}
	
	  /**
     * 计算当前月加减
     * @param months + or -1数字
     * @return 日期
     */
	public static String getMonths(int months) {
		Date d = new Date();
		GregorianCalendar gc = new GregorianCalendar();
		SimpleDateFormat sf = new SimpleDateFormat(YYYYMMDDHHMMSS_19);
		gc.setTime(d);
		gc.add(2, months);
//		System.out.println(gc.get(Calendar.YEAR));
/*		gc.set(gc.get(Calendar.YEAR), gc.get(Calendar.MONTH),
				gc.get(Calendar.DATE));*/
		return sf.format(gc.getTime());
	}
	/**
	 * 计算当前日加减
	 * @param months + or -1数字
	 * @return 日期
	 */
	public static String getDays(int days) {
		Date d = new Date();
		GregorianCalendar gc = new GregorianCalendar();
		SimpleDateFormat sf = new SimpleDateFormat(YYYYMMDDHHMMSS_19);
		gc.setTime(d);
		gc.add(5, days);
//		System.out.println(gc.get(Calendar.YEAR));
		/*		gc.set(gc.get(Calendar.YEAR), gc.get(Calendar.MONTH),
				gc.get(Calendar.DATE));*/
		return sf.format(gc.getTime());
	}


	public static void main(String[] args) throws ParseException {
		
		/**java中对日期的加减操作
		*gc.add(1,-1)表示年份减一.
		*gc.add(2,-1)表示月份减一.
		*gc.add(3.-1)表示周减一.
		*gc.add(5,-1)表示天减一.
		*以此类推应该可以精确的毫秒吧.没有再试.大家可以试试.
		*GregorianCalendar类的add(int field,int amount)方法表示年月日加减.
		*field参数表示年,月.日等.
		*amount参数表示要加减的数量.*/
	/*	Date date = new Date();
		GregorianCalendar gc2= new GregorianCalendar();
		gc2.setTime(date);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		gc2.add(1, 0);
		String d=sdf.format(gc2.getTime());
		System.out.println(d);*/
	/*	
		GregorianCalendar gc=new GregorianCalendar();
        
        try {
            gc.setTime( new SimpleDateFormat("yyyyMM").parse("200901"));
            gc.add(2, -Integer.parseInt("7"));
        } catch (Exception e) {
            // TODO 自動生成された catch ブロック
            e.printStackTrace();
        }
        System.out.println(new SimpleDateFormat("yyyyMM").format(gc.getTime()));*/
		 Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDDHHMMSS_19);
		String dd=sdf.format(d);
		String ddd =getMonths(1);
		System.out.println(ddd);

	}
		
		@Test
		public void dff() throws ParseException{
			int years=0;
			int months=0;
			int days=0;
			int hours=0;
			int minutes=0;
			int seconds=0;
			SimpleDateFormat sdf=new SimpleDateFormat(YYYYMMDDHHMMSS_19);
			Date willDate=sdf.parse("2015-01-12 15:00:00");
			Calendar cal = Calendar.getInstance();
			cal.setTime(willDate);
			 cal.add(cal.YEAR,years);
				 cal.add(cal.MONTH,months);
				 cal.add(cal.DAY_OF_MONTH,days);
				 cal.add(cal.HOUR_OF_DAY,hours);
				 cal.add(cal.MINUTE,minutes);
				 cal.add(cal.SECOND,seconds);
//				 System.out.println(cal.get(cal.YEAR));
//				 System.out.println(cal.get(cal.DATE));
			String d=sdf.format(cal.getTime());
			System.out.println(d);
		}
		
		@Test
		public void fdr() throws ParseException{
			System.out.println(getAirthStringDateHM(new Date(), 0, 0));
		}
		
		
//		public static void main(String[] args) throws Exception {		
//		String startDate = "1987-04-13";		
//		String endDate = "2012-01-23";		
//		System.out.println(startDate + "锟斤拷" + endDate + "之锟斤拷锟斤拷锟� + diffMonth(startDate, endDate) + "锟斤拷锟斤拷");	
//		}	
	/**
	 * 两月日期时间差
	* @Description: TODO(描述) 
	* @param  设定
	* @author 作者 jacking 
	* @date 2016-7-5 上午9:34:54 
	* @return 天数
	 */
	public static int diffMonth(String startDate, String endDate){	 
		int[] date1 = convertStr(startDate);		
		int[] date2 = convertStr(endDate);	
		int result = 0;		 
		result += (date2[0] - date1[0]) * 12;		 
		result += (date2[1] - date1[1]);		 	
		result += (date2[2] > date1[2] ? 0 : -1);		
		return result;
		}	
	/**
	 * 转换日期时间为天
	* @Description: TODO(描述) 
	* @param  设定
	* @author 作者 jacking 
	* @date 2016-7-5 上午9:34:54 
	* @return 天数
	 */
	public static int[] convertStr(String str) {
		String[] strArr = str.split("-");		
		int[] result = new int[strArr.length];		
		for(int i = 0; i < strArr.length; i++) {	
			result[i] = Integer.parseInt(strArr[i]);		
			}		
		return result;	
		}
	
	/**
	 * 两个字符日期相差天数
	* @Description: TODO(描述) 
	* @param  设定
	* @author 作者 jacking 
	* @date 2016-7-5 上午9:38:38 
	* @return
	 */
	public static long getQuot(String time1, String time2){
		long quot = 0;  
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");  
		try {   Date date1 = ft.parse( time1 );
		Date date2 = ft.parse( time2 ); 
		quot = date1.getTime() - date2.getTime(); 
		quot = quot / 1000 / 60 / 60 / 24;  
		} catch (ParseException e) { 
		e.printStackTrace();
		}  
		return quot; 
		}
		
		
}